﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;  
using System.Text; 

public partial class MakeLotery : System.Web.UI.Page
{
    //public QualifyRounds oQualifyRounds = new QualifyRounds();
    public Tournament oTournament;
    //public Boolean IsGenerated= false;
   // public int Pk_Tournament;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            int Pk_Tournament = int.Parse(Request.QueryString["id"]);
            oTournament = new Tournament(Pk_Tournament);
            Session["oTournament"] = oTournament;
            if (!Page.IsPostBack)
            {
             
              
               
               Session["IsGenerated"] = false;
               okmsg.Visible = false;
               badmsg.Visible = false;
               //IsGenerated = false;
            }

            //  Boolean res = QualifyPlayersMachines.SaveQualify(oQualifyRound, Pk_Tournament);
            //oQualifyRound.Sort(new QualifyPlayersComparer(Order.Ascending)); 
            
        }
        catch
        { 
        
        }

    }
    public string drawTable() {
        Tournament oTournament = (Tournament)Session["oTournament"];
        QualifyRounds oQualifyRounds = (QualifyRounds)Session["oQualifyRounds"];

        //Control de generacion

        //Vemos si hay suficientes máquinas para el torneo.
        int TotalPlayers = Player.GetPlayersByTournament(oTournament.Pk_Tournament).Count;
        int Totalmachines = Machine.GetMachinesByTournament(oTournament.Pk_Tournament).Count;
        int Totalrounds = oTournament.QualifyRounds;
        decimal value = (TotalPlayers / 4);
        decimal MachinesNeeded = Math.Floor(value);
        if (oTournament.TournamentType == 0)
        {
        //Vemos si hay suficientes máquinas:
        if ((TotalPlayers / 4) < Totalmachines) {
            //No hay suficientes maquinas
            StringBuilder OutPutHTML = new StringBuilder();
            OutPutHTML.Append("<table class=\"Lotery\" style=\"width:100%;text-align:left;border:1px solid black;border-collapse: collapse; padding:5px\">");
            OutPutHTML.Append("<tr style=\"background-color:#ffffff;border:1px solid black; font-weight:bold\">");
            //Header
            OutPutHTML.Append("<td>There isn't enought machines you need one machine per 4 players, per round. You have selected " + TotalPlayers + " players and only " + Totalmachines + " machines, you need " + MachinesNeeded.ToString()  + " machines</td>");
            return OutPutHTML.ToString() ;
        }  
        //Vemos si hay exceso de maquinas.
        
            if (Totalmachines < (TotalPlayers / 4))
            {
                //No hay suficientes maquinas
                StringBuilder OutPutHTML = new StringBuilder();
                OutPutHTML.Append("<table class=\"Lotery\" style=\"width:100%;text-align:left;border:1px solid black;border-collapse: collapse; padding:5px\">");
                OutPutHTML.Append("<tr style=\"background-color:#ffffff;border:1px solid black; font-weight:bold\">");
                //Header
                OutPutHTML.Append("<td>There more  machines than you need, one machine per 4 players, per round. You have selected " + TotalPlayers + " players and only " + Totalmachines + " machines, you need " + MachinesNeeded.ToString() + " machines</td>");
                return OutPutHTML.ToString();
            }
            if (TotalPlayers > (Totalmachines * 4) - 4)
            {
                //Falta jugadores
                StringBuilder OutPutHTML = new StringBuilder();
                OutPutHTML.Append("<table class=\"Lotery\" style=\"width:100%;text-align:left;border:1px solid black;border-collapse: collapse; padding:5px\">");
                OutPutHTML.Append("<tr style=\"background-color:#ffffff;border:1px solid black; font-weight:bold\">");
                //Header
                OutPutHTML.Append("<td>There more  machines than you need, one machine per 4 players, per round. You have selected " + TotalPlayers + " players and only " + Totalmachines + " machines, you need " + MachinesNeeded.ToString() + " machines</td>");
                return OutPutHTML.ToString();
            }
        }
        else {
            if (Totalmachines < oTournament.QualifyRounds) {
                StringBuilder OutPutHTML = new StringBuilder();
               
                OutPutHTML.Append("<table class=\"Lotery\" style=\"width:100%;text-align:left;border:1px solid black;border-collapse: collapse; padding:5px\">");
                OutPutHTML.Append("<tr style=\"background-color:#ffffff;border:1px solid black; font-weight:bold\">");
                //Header
                OutPutHTML.Append("<td>You have " + Totalmachines + " machines, you need almost " + oTournament.QualifyRounds + " machines for " + oTournament.QualifyRounds + " rounds </td>");
                return OutPutHTML.ToString();
            } 
        }
      
        if (Boolean.Parse(Session["IsGenerated"].ToString()) == true)
        {
            if (oTournament.TournamentType == 0)
            {
                StringBuilder OutPutHTML = new StringBuilder();
                OutPutHTML.Append("<table class=\"Lotery\" style=\"width:100%;text-align:left;border:1px solid black;border-collapse: collapse; padding:5px\">");
                OutPutHTML.Append("<tr style=\"background-color:#ffffff;border:1px solid black; font-weight:bold\">");
                //Header
                OutPutHTML.Append("<td>Player</td>");
                for (int x = 0; x < oTournament.QualifyRounds; x++)
                {
                    OutPutHTML.AppendFormat("<td>Round {0}</td>", x + 1);
                }
                OutPutHTML.Append("<td style=\"border-left:solid 1px black\">Player</td>");
                for (int x = 0; x < oTournament.QualifyRounds; x++)
                {
                    OutPutHTML.AppendFormat("<td>Round {0}</td>", x + 1);
                }
                OutPutHTML.Append("</tr>");
                int RowColor = 0;
                int NextPlayer = 0;
                int NextRow = 0;
                //Recorremos todas las rondas
                for (int x = 0; x < oQualifyRounds.Rounds.Count; x++)
                {
                    if (NextPlayer == 0 && NextRow == 0)
                    {

                        if (RowColor % 2 == 0)
                        {
                            OutPutHTML.Append("<tr style=\"background-color:#c2c2c2\">");
                        }
                        else
                        {
                            OutPutHTML.Append("<tr style=\"background-color:#ffffff\">");
                        }

                        //Pintamos el jugador

                    }
                    if (NextPlayer == 0)
                    {
                        OutPutHTML.AppendFormat("<td style=\"border-left:solid 1px black\">{0}-{1}</td>", oQualifyRounds.Rounds[x].PlayerNumber, oQualifyRounds.Rounds[x].Player_Nick);
                    }
                    //siempre se van pintando las máquinas
                    OutPutHTML.AppendFormat("<td>{0}-{1} ({2}º)</td>", oQualifyRounds.Rounds[x].Machine_Number, oQualifyRounds.Rounds[x].Machine_Acron, oQualifyRounds.Rounds[x].PlayInPosition);

                    NextPlayer++;
                    if (NextPlayer >= oTournament.QualifyRounds)
                    {
                        NextRow++;
                        NextPlayer = 0;
                    }

                    if (NextRow > 1)
                    {
                        NextRow = 0;
                        OutPutHTML.Append("</tr>");
                        RowColor++;
                    }



                }



                OutPutHTML.Append("</table>");
                return OutPutHTML.ToString();
            }
            else { 
                //free tournament
                StringBuilder OutPutHTML = new StringBuilder();
                OutPutHTML.Append("<table class=\"Lotery\" style=\"width:100%;text-align:left;border:1px solid black;border-collapse: collapse; padding:5px\">");
                OutPutHTML.Append("<tr style=\"background-color:#ffffff;border:1px solid black; font-weight:bold\">");
                OutPutHTML.AppendFormat("<td>PLAYERS: ");
                for (int x = 0; x < oQualifyRounds.Rounds.Count; x++)
                {
                    OutPutHTML.AppendFormat("Machine: {0}, Player {1} ({2}) - ", oQualifyRounds.Rounds[x].Machine_Number, oQualifyRounds.Rounds[x].Player_Nick, oQualifyRounds.Rounds[x].PlayerNumber);
                }
                OutPutHTML.AppendFormat("</td>");
                OutPutHTML.Append("</tr>");
                OutPutHTML.Append("</table>");
                return OutPutHTML.ToString();
            }
        }
        else {
            return string.Empty; 
        }
    }
    protected void bt_save_ServerClick(object sender, EventArgs e)
    {
        try
        {
            okmsg.Visible =false;
            badmsg.Visible = false;
            QualifyRounds oQualifyRounds = (QualifyRounds)Session["oQualifyRounds"];
            Boolean result = QualifyRounds.SaveQualify(oQualifyRounds.Rounds);
            if (result == true)
            {
                okmsg.Visible = true;

            }
            else {
                badmsg.Visible = true; 
            }

        }
        catch{ 
        
        } 
    }
    protected void bt_generate_ServerClick(object sender, EventArgs e)
    {
        try
        {
            Tournament oTournament = (Tournament)Session["oTournament"];
            List<Machine> oMachine = Machine.GetMachinesByTournament(oTournament.Pk_Tournament);
            List<Player> oPlayer = Player.GetPlayersByTournament(oTournament.Pk_Tournament);
            QualifyRounds oQualifyRounds = new QualifyRounds();
            
            Lotery.MakeQualify(oMachine, oPlayer, oTournament, ref oQualifyRounds);
            Session["IsGenerated"] = true;// IsGenerated = true;
            dados.Visible = false;
            //oQualifyRounds.Rounds.Sort(new QualifyRoundPlayerComparer(Order.Ascending));
            oQualifyRounds.Rounds.Sort(new ObjectComparer<QualifyRound>("PlayerNumber ASC, Round_Number ASC", true));
            Session["oQualifyRounds"] = oQualifyRounds; //public QualifyRounds oQualifyRounds = new QualifyRounds();
                
        }
        catch(Exception Ex) {
            throw Ex;
        } 
    }
}
